package com.lnsf.bookstore.system.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lnsf.bookstore.system.user.dao.UserMapper;
import com.lnsf.bookstore.system.user.dao.UserRoleMapper;
import com.lnsf.bookstore.system.user.service.UserService;
import com.lnsf.bookstore.system.user.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private UserRoleMapper userRoleMapper;

	/**
	 * shiro myrealm验证
	 * @param username
	 * @return
	 */
	public User getByUserName(String username) {
		return userMapper.getUserByName(username);
	}

	/**
	 * 后台会员 列表
	 * @param map
	 * @param user
	 * @return
	 */
	/*@Override
	public PageInfo<VipUser> selectAllVip(Map<String, Object> map, VipUser user) {
		PageHelper.startPage(Integer.parseInt(map.get("page").toString()),Integer.parseInt(map.get("limit").toString()));
		//很多种会员
		map.put("roleName","admin");
		return  new PageInfo<VipUser>(userMapper.selectAll(map));
	}*/

	/**
	 * 管理员 列表
	 * @return
	 */
	@Override
	public PageInfo<User> selectAllAdmin(Map<String, Object> map, User user) {
		PageHelper.startPage(Integer.parseInt(map.get("page").toString()),Integer.parseInt(map.get("limit").toString()));
		return  new PageInfo<User>(userMapper.selectAllAdmin(map));
	}

	/**
	 * 管理员添加，需要给一个角色
	 * @param user
	 * @return
	 */
	public Integer saveUser(User user) {
		int result = 1;
		//先保存
		userMapper.insertSelective(user);
		if(user.getRoleIdList().size() == 0){
			return 0;
		}
		//删除用户角色表数据
		userRoleMapper.deleteByUserId(user.getId());
		//保存用户角色表数据
		Map<String, Object> map = new HashMap<>();
		map.put("userId", user.getId());
		if(user.getRoleIdList().size()!=0) {
			map.put("roleIdList", user.getRoleIdList());
			userRoleMapper.saveOrUpdate(map);
		}
		return  result;

	}

	public Integer updateUser(User user) {
		int result = 1;
		//先更新
		userMapper.updateByPrimaryKeySelective(user);
		if(user.getRoleIdList().size() == 0){
			return 0;
		}
		//删除用户角色表数据
		userRoleMapper.deleteByUserId(user.getId());
		//保存用户角色表数据
		Map<String, Object> map = new HashMap<>();
		map.put("userId", user.getId());
		if(user.getRoleIdList().size()!=0) {
			map.put("roleIdList", user.getRoleIdList());
		  	userRoleMapper.saveOrUpdate(map);
		}
		return result;

	}

	@Override
	public void deleteBatch(Integer[] userIds) {
		userMapper.deleteBatch(userIds);
	}

	@Override
	public User selectById(Integer userId) {
		User user = userMapper.selectByPrimaryKey(userId);
		//获取用户所属的角色列表
		List<Integer> roleIdList =userRoleMapper.listRoleIdByUserId(userId);
		user.setRoleIdList(roleIdList);
		return user;
	}


	public User getUserByName_roleAndPer(String username) {
		return userMapper.getUserByName_roleAndPer(username);
	}


	public User getByPhone(String phone) {
		// TODO Auto-generated method stub
		return userMapper.getUserByPhone(phone);
	}

	/**
	 * 修改登陆者密码
	 * @param userId
	 * @param password
	 * @param newPassword
	 * @return
	 */
	@Override
	public int updatePassword(Integer userId, String password, String newPassword) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId",userId);
		map.put("password",password);
		map.put("newPassword",newPassword);
		return userMapper.updatePassword(map);
	}




}
